#include<vector>
#include<algorithm>
using namespace std;

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if (prices.size() < 2) return 0;
        int res = 0;
        int cur_max = prices.back();
        for (int i = prices.size() - 2; i >= 0; --i) {
            if (cur_max < prices[i])
                cur_max = prices[i];
            else {
                res = max(res, cur_max - prices[i]);
            }
        }
        return res;
    }
};